<?php
// +----------------------------------------------------------------------
// | 零云 [ 简单 高效 卓越 ]
// +----------------------------------------------------------------------
// | Copyright (c) 2016 http://www.lingyun.net All rights reserved.
// +----------------------------------------------------------------------
// | Author: jry <598821125@qq.com>
// +----------------------------------------------------------------------
namespace Admin\Model;

use Common\Model\ModelModel;

/**
 * 部门模型
 * @author jry <598821125@qq.com>
 */
class ApproveModel extends ModelModel
{
    /**
     * 数据库表名
     * @author jry <598821125@qq.com>
     */
    protected $tableName = 'admin_approve';

    /**
     * 自动验证规则
     * @author jry <598821125@qq.com>
     */
    protected $_validate = array(
        array('approve_advice', 'require', '审核意见不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
        array('approve', 'require', '审核结果不能为空', self::MUST_VALIDATE, 'regex', self::MODEL_BOTH),
    );

    /**
     * 自动完成规则
     * @author jry <598821125@qq.com>
     */
    protected $_auto = array(
        array('create_time', 'time', self::MODEL_INSERT, 'function'),
        array('update_time', 'time', self::MODEL_BOTH, 'function'),
        array('status', '1', self::MODEL_INSERT),
    );

    protected function _before_insert(&$data,$options) {
        
        $now_status = D('Admin/Course')->getFieldById($data['course_id'],'approve');
        $c_data['approve'] = $now_status;
        switch ($data['approve']) {
            case '1'://通过
                if ($now_status<3) {
                    $c_data['approve'] = $now_status+1;
                }
            break;
            case '2'://驳回
                if ($now_status>0) {
                    $c_data['approve'] = 0;
                }
            break;
        }
        $cond['id'] = $data['course_id'];
        $c_data['update_time'] = time();
        D('Admin/Course')->where($cond)->data($c_data)->save();
        

    }

    protected function _after_find(&$result, $options)
    {
        $result['user_name'] = D('Admin/User')->getFieldById($result['user_id'],'nickname');
        $result['approve_result'] = $result['approve']==1?'通过':'驳回';
        $result['approve_advice'] = nl2br($result['approve_advice']);
    }
    protected function _after_select(&$result, $options)
    {
        foreach ($result as &$record) {
            $this->_after_find($record, $options);
        }
    }
    
}
